package com.org.eventcenter.eventcenter.process;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.org.eventcenter.eventcenter.service.DataFuseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Evil Lee
 * @创建时间 2020/4/7
 * @描述
 */
@Component
@Slf4j
public class EventCenterProcess {


    @Autowired
    DataFuseService dataFuseService;


   // @KafkaListener(topics ="${newTopicName}", containerFactory = "batchFactory")
    public void listen2(List<ConsumerRecord<String, String>> records) {
        batchConsumer(records);
    }
 /*   @KafkaListener(topics ="${topicName}", containerFactory = "batchFactory")
    public void listen2(List<ConsumerRecord<String, String>> records) {
        batchConsumer(records);
    }*/

    /**
     * 批量消费
     */
    public void batchConsumer(List<ConsumerRecord<String, String>> records) {
        records.forEach(record -> {
            try {
                consumer(record);
            } catch (Exception e) {
                log.error("KAFKA 批量消费失败"+e.getMessage());
            }
        });
    }


    public void consumer(ConsumerRecord<String, String> record) throws Exception{
        log.info("主题:{}, 内容: {}", record.topic(), record.value());
        //dataFuseService.dataFuse(json,type);
    }
}

